package com.itic.market.mobile.uc.group;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.itic.appbase.framework.common.constants.DBConst;
import com.itic.appbase.framework.utils.StringHelper;
import com.itic.market.mobile.base.BaseMobileInterface;
import com.itic.market.system.user.persistence.model.MarketUser;
import com.itic.market.system.user.persistence.model.MarketUserExample;
import com.itic.market.system.user.persistence.model.MarketUserExample.Criteria;


/**
 * 用户设置接口类
 * @author 许小宝
 * 2017年07月26日  14时11分
 */
@Controller
@RequestMapping(value="/mobile/uc/group")
public class MarketUcGroupInterface
     extends BaseMobileInterface {
	
	
	/**
     * 获取当前团队信息
     * @return 
     */
    @RequestMapping(value="/index")
    @ResponseBody
    public Map<String,Object> index( HttpServletRequest request, HttpServletResponse response, String token ){
    	Map<String, Object> resultMap = new HashMap<String,Object>();
    	String result = FAIL;
    	String errorCode = PARAM_ERROR_CODE;
    	String errorMsg = PARAM_ERROR_MSG;
    	try{
    		if( StringHelper.isNotBlank(token) ){
    			MarketUser user = getMarketUserByToken(token);
    			if( null != user ){
    				MarketUserExample example = new MarketUserExample();
    				Criteria criteria = example.createCriteria();
    				criteria.andIsEffectiveEqualTo(DBConst.TRUE);
    				criteria.andInvitingIdEqualTo(user.getId());
    				int count = marketUserService.count(example);
    				
    				List<MarketUser> list = marketUserService.list(example);
    				List<String> ids = new ArrayList<String>();
    				if( list.size() > 0 ){
    					for( MarketUser temp: list ){
    						ids.add(temp.getId());
    					}
    					example.clear();
        				example.createCriteria().andIsEffectiveEqualTo(DBConst.TRUE).andInvitingIdIn(ids);
        				int count2 = marketUserService.count(example);
        				if( count2>0 ){
        					count += count2;
        				}
    				}
    				
    				Map<String, Object> dataMap = new HashMap<String,Object>();
    				dataMap.put("groupCode", user.getInvitingCode());
    				dataMap.put("groupCount", count);
    				//判断必要的参数
    				resultMap.put(DATA, dataMap);
    				result = SUCCESS;
    				errorCode = SUCCESS_ERROR_CODE;
    	        	errorMsg = SUCCESS_ERROR_MSG; 
    			}else{
    				result = LOGIN;
    				errorCode = LOGIN_ERROR_CODE;
    	        	errorMsg = LOGIN_ERROR_MSG; 
    			}
    		}
    		
    		
        	
    	}catch(Exception e){
    		errorCode = SERVER_ERROR_CODE;
        	errorMsg = SERVER_ERROR_MSG;
        	log(e);
    	}
    	resultMap.put(RESULT, result);
    	resultMap.put(ERROR_CODE, errorCode);
    	resultMap.put(ERROR_MSG, errorMsg);
    	isCrrossDomain(response);
    	return resultMap;
    } 
    
    /**
     * 获取团队中有多少人
     * @return 
     */
    @RequestMapping(value="/peoples")
    @ResponseBody
    public Map<String,Object> peoples( HttpServletRequest request, HttpServletResponse response, String token,String type ){
    	Map<String, Object> resultMap = new HashMap<String,Object>();
    	String result = FAIL;
    	String errorCode = PARAM_ERROR_CODE;
    	String errorMsg = PARAM_ERROR_MSG;
    	int count1 = 0;
    	int count2 = 0;
    	try{
    		if( StringHelper.isNotBlank(token) ){
    			
    			MarketUser user = getMarketUserByToken(token);
    			if( null != user ){
    				
    				if( StringHelper.isNotBlank(type) && "2".equals(type) ){//这是二级店主
    					MarketUserExample example = new MarketUserExample();
        				Criteria criteria = example.createCriteria();
        				criteria.andIsEffectiveEqualTo(DBConst.TRUE);
        				criteria.andInvitingIdEqualTo(user.getId());
        				count1 = marketUserService.count(example);
        				List<MarketUser> list = marketUserService.list(example);
        				List<String> ids = new ArrayList<String>();
        				if( list.size() > 0 ){
        					for( MarketUser temp: list ){
        						ids.add(temp.getId());
        					}
        					example.clear();
            				example.setOrderByClause("CREATE_TIME DESC");
            				example.createCriteria().andIsEffectiveEqualTo(DBConst.TRUE).andInvitingIdIn(ids);
            				//this.handlePageQueryDefault(request, example);
            				count2 = marketUserService.count(example);
            			    list = marketUserService.list(example);
        				}
        				
        				//判断必要的参数
        				resultMap.put(DATA, list);
        				result = SUCCESS;
        				errorCode = SUCCESS_ERROR_CODE;
        	        	errorMsg = SUCCESS_ERROR_MSG; 
        	        	
        			}else{//默认是一级
        				MarketUserExample example = new MarketUserExample();
        				example.setOrderByClause("CREATE_TIME DESC");
        				Criteria criteria = example.createCriteria();
        				criteria.andIsEffectiveEqualTo(DBConst.TRUE);
        				criteria.andInvitingIdEqualTo(user.getId());
        				//this.handlePageQueryDefault(request, example);
        			    count1 = marketUserService.count(example);
        				List<MarketUser> list = marketUserService.list(example);
        				if( list.size() > 0 ){
        					for( MarketUser temp: list ){
        						if( StringHelper.isBlank(temp.getUserPhone()) ){
        							temp.setUserPhone("暂未绑定");
        						}
        						if( StringHelper.isBlank(temp.getUserNickName()) ){
        							temp.setUserNickName("未知用户");
        						}
        					}
        				}
        				
        				List<String> ids = new ArrayList<String>();
        				if( list.size() > 0 ){
        					for( MarketUser temp: list ){
        						ids.add(temp.getId());
        					}
        					example.clear();
            				example.createCriteria().andIsEffectiveEqualTo(DBConst.TRUE).andInvitingIdIn(ids);
            				count2 = marketUserService.count(example);
        				}
        				//判断必要的参数
        				resultMap.put(DATA, list);
        				
        				result = SUCCESS;
        				errorCode = SUCCESS_ERROR_CODE;
        	        	errorMsg = SUCCESS_ERROR_MSG; 
        			}
    				
    			}else{
    				result = LOGIN;
    				errorCode = LOGIN_ERROR_CODE;
    	        	errorMsg = LOGIN_ERROR_MSG; 
    			}
    		}
    		
    		
        	
    	}catch(Exception e){
    		errorCode = SERVER_ERROR_CODE;
        	errorMsg = SERVER_ERROR_MSG;
        	log(e);
    	}
    	resultMap.put(RESULT, result);
    	resultMap.put(ERROR_CODE, errorCode);
    	resultMap.put(ERROR_MSG, errorMsg);
    	resultMap.put("count1", count1);
    	resultMap.put("count2", count2);
    	isCrrossDomain(response);
    	return resultMap;
    } 
    
	
    
    
}